<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2010 - 2011         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Defaults extends Controller
{
    function __construct()
    {
        parent::Controller();
        $this->lang->load('home/global');
    }
    
    function index()
    {
        #BEGIN: Select Product By Section
        $sql = "SELECT secID FROM {PRE}section ";
        $sql .= "WHERE secStatus = 1 AND secType = 1 ";
        $sql .= "ORDER BY RAND()";
        $sections = $this->global_model->fetch($sql);
        $idSections = array();
        foreach($sections as $section)
        {
            $idSections[] = $section->secID;
        }
        if(count($idSections) >= 4)
        {
            $selectSections = explode(',', SECTION_SELECT_HOME);
            $selectSection1 = (isset($selectSections[0]) && in_array($selectSections[0], $idSections))?$selectSections[0]:$idSections[0];
            $selectSection2 = (isset($selectSections[1]) && in_array($selectSections[1], $idSections))?$selectSections[1]:$idSections[1];
            $selectSection3 = (isset($selectSections[2]) && in_array($selectSections[2], $idSections))?$selectSections[2]:$idSections[2];
            $selectSection4 = (isset($selectSections[3]) && in_array($selectSections[3], $idSections))?$selectSections[3]:$idSections[3];
            $sql = "SELECT * FROM {PRE}product ";
            $sql .= "INNER JOIN {PRE}user ON {PRE}product.proUser = {PRE}user.useID ";
            $sql .= "INNER JOIN {PRE}shop ON {PRE}user.useID = {PRE}shop.shoUser ";
            $sql .= "INNER JOIN {PRE}section ON {PRE}product.proSection = {PRE}section.secID ";
            $sql .= "INNER JOIN {PRE}province ON {PRE}product.proProvince = {PRE}province.preID ";
            $sql .= "WHERE proImage != '' AND proStatus = 1 AND proExpireDate >= ".time()." ";
            $sql1 = $sql."AND proSection = ".(int)$selectSection1." ";
            $sql1 .= "ORDER BY proOrder ASC, proID DESC ";
            $sql2 = $sql."AND proSection = ".(int)$selectSection2." ";
            $sql2 .= "ORDER BY proOrder ASC, proID DESC ";
            $sql3 = $sql."AND proSection = ".(int)$selectSection3." ";
            $sql3 .= "ORDER BY proOrder ASC, proID DESC ";
            $sql4 = $sql."AND proSection = ".(int)$selectSection4." ";
            $sql4 .= "ORDER BY proOrder ASC, proID DESC ";
            $data['introProduct1'] = $this->global_model->get($sql1);
            $data['moreProducts1'] = $this->global_model->fetch($sql1."LIMIT 1, 8");
            $data['introProduct2'] = $this->global_model->get($sql2);
            $data['moreProducts2'] = $this->global_model->fetch($sql2."LIMIT 1, 8");
            $data['introProduct3'] = $this->global_model->get($sql3);
            $data['moreProducts3'] = $this->global_model->fetch($sql3."LIMIT 1, 8");
            $data['introProduct4'] = $this->global_model->get($sql4);
            $data['moreProducts4'] = $this->global_model->fetch($sql4."LIMIT 1, 8");
        }
        #END Select Product By Section
        #Load View
        $this->load->view('home/defaults/defaults', $data);
    }
}